/*
 * DespachosContextListener.java
 *
 * Created on 26 de noviembre de 2007, 12:03 PM
 */

package despachosportletpanelcontrol.listener;

import com.src.computec.radar.despachos.base.constantes.ConstantesGenerales;
import com.src.log4j.logger.custom.XLogger;
import java.io.File;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;

/**
 *
 * @author  aocampo
 * @version
 *
 * Web application lifecycle listener.
 */

public class DespachosContextListener implements ServletContextListener {
    
    private static XLogger xLogger;

    public DespachosContextListener() {
        this.xLogger = (XLogger) XLogger.getLogger(this.getClass().getName());
    }

    /**
     * ### Method from ServletContextListener ###
     * 
     * Called when a Web application is first ready to process requests
     * (i.e. on Web server startup and when a context is added or reloaded).
     * 
     * For example, here might be database connections established
     * and added to the servlet context attributes.
     */
    public void contextInitialized(ServletContextEvent evt) {
        // TODO add your code here e.g.:
        /*
            Connection con = // create connection
            evt.getServletContext().setAttribute("con", con);
        */
        xLogger.info("DespachosContextListener:contextInitialized");
        int numberFilesDeleted = 0;
        try {
            File directory = new File(ConstantesGenerales.getInstance().PATH_VIEW_LABEL);
            if (directory.exists() && directory.isDirectory()) {
                String[] arrayNamesFiles = directory.list();
                for (int i=0; i<arrayNamesFiles.length; i++) {
                    File file = new File(ConstantesGenerales.getInstance().PATH_VIEW_LABEL + arrayNamesFiles[i]);
                    file.delete();
                    numberFilesDeleted++;
                }
            }
            xLogger.info("DespachosContextListener:contextInitialized:numberFilesDeleted:" + numberFilesDeleted);
        }
        catch (Exception e) {
            xLogger.error("DespachosContextListener:contextInitialized:", e);
        }
    }

    /**
     * ### Method from ServletContextListener ###
     * 
     * Called when a Web application is about to be shut down
     * (i.e. on Web server shutdown or when a context is removed or reloaded).
     * Request handling will be stopped before this method is called.
     * 
     * For example, the database connections can be closed here.
     */
    public void contextDestroyed(ServletContextEvent evt) {
        // TODO add your code here e.g.:
        /*
                Connection con = (Connection) e.getServletContext().getAttribute("con");
                try { con.close(); } catch (SQLException ignored) { } // close connection
        */
    }
}